package com.usian.springbootmysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.springbootmysql.dao.CollegeDao;
import com.usian.springbootmysql.pojo.ResultVo;
import com.usian.springbootmysql.pojo.SearchDao;
import com.usian.springbootmysql.pojo.TbCollege;
import com.usian.springbootmysql.service.CollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeDao, TbCollege> implements CollegeService {

    @Autowired
    private CollegeDao collegeDao;

    @Override
    public ResultVo saveCollege(TbCollege tbCollege) {
        //添加之前，需要判断方向是否存在，如果存在，则不允许添加
//        QueryWrapper<TbCollege> query = new QueryWrapper<TbCollege>();
//        query.eq("name",tbCollege.getName());
        LambdaQueryWrapper<TbCollege> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbCollege::getName, tbCollege.getName());
        int count = collegeDao.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            return new ResultVo(200, "名称存在，不允许添加", null);
        } else {
            collegeDao.insert(tbCollege);
            return new ResultVo(200, "添加成功", null);
        }
    }

    @Override
    public List<TbCollege> search(String status) {
        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();
        query.eq(TbCollege::getStatus, status);
        List<TbCollege> list = collegeDao.selectList(query);
        return list;
    }

    @Override
    public List<TbCollege> searchVo(TbCollege tbCollege) {
        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();
        if (StringUtils.isEmpty(tbCollege.getName())) {
            query.like(TbCollege::getName, tbCollege.getName());
        }
        if (tbCollege.getStatus()!=null) {
            query.eq(TbCollege::getStatus, tbCollege.getStatus());
        }
        List<TbCollege> list = collegeDao.selectList(query);
        return list;
    }

    @Override
    public IPage<TbCollege> searchPage(SearchDao searchDao) {
        IPage<TbCollege> page = new Page<TbCollege>(searchDao.getPage(),searchDao.getSize());
        LambdaQueryWrapper<TbCollege> query = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchDao.getName())) {
            query.like(TbCollege::getName, searchDao.getName());
        }
//        if (searchDao.getStatus()!=null) {
//            query.eq(TbCollege::getStatus, searchDao.getStatus());
//        }
        IPage<TbCollege> list = collegeDao.selectPage(page,query);
        return list;
    }
}

























